package com.ebt.m.customer.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;

/**
 * Key:Value形式的文本
 * Created by archer.qi on 2016/12/12.
 */
public class PairView extends LinearLayout {

    private TextView mTvKey;
    private TextView mTvValue;

    private static final String DIV = " :  ";

    private String mStrKey;
    private int mColorKey;
    private float mSizeKey;
    private int mColorValue;
    private float mSizeValue;

    public PairView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.view_pair, null);
        addView(contentView);

        mTvKey = (TextView) contentView.findViewById(R.id.pair_key);
        mTvValue = (TextView) contentView.findViewById(R.id.pair_value);

        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.PairView);
        mStrKey = t.getString(R.styleable.PairView_key_text);
        setKetText(mStrKey);

        mColorKey = t.getColor(R.styleable.PairView_keyColor, Color.parseColor("#ff666666"));
        mSizeKey = t.getColor(R.styleable.PairView_keySize, 12);
        mTvKey.setTextColor(mColorKey);
        mTvKey.setTextSize(mSizeKey);

        mColorValue = t.getColor(R.styleable.PairView_valueColor, Color.parseColor("#ff444444"));
        mSizeValue = t.getColor(R.styleable.PairView_valueSize, 12);
        mTvValue.setTextColor(mColorValue);
        mTvValue.setTextSize(mSizeValue);
    }

    public void setKetText(String key) {
        if (!TextUtils.isEmpty(key)) {
            mTvKey.setText(key + DIV);
        }
    }

    public void setValueText(String value) {
        mTvValue.setText(getEmptyFilterText(value));
    }

    private String getEmptyFilterText(String val) {
        if (val == null || "null".equals(val) || val.length() == 0) {
            return "";
        }
        return val;
    }

}
